// 引入Vue Vuex
import Vue from 'vue'
import Vuex from 'vuex'

// Vue 与 Vuex 相关联
Vue.use(Vuex)

// 创建对象 
const store = new Vuex.Store({
    // staore  仓库数据
    state: {
        cartList: uni.getStorageSync("cartList")|| [],
    },
    // 计算属性
    getters: {
        // 计算总数量
        count: state => {
            let count = 0
            state.cartList.forEach(goods => {
                if(goods.checked) {
                    count += goods.count
                }
            })
            return count
        },
         // 计算总价格
        allPrice: state => {
            let allPrice = 0
            state.cartList.forEach((goods)=>{
                if(goods.checked){
                    allPrice += goods.goods_price * goods.count
                }
            })
            return allPrice
        },
    },
    mutations: {
        setCartList(state, cartList) {
            state.cartList = cartList
            uni.setStorageSync('cartList', cartList)
        }
    }
    
    
})

// 导出
export default store